package com.ld.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ld.dao.MongoBannerNDao;
import com.ld.dao.MysqlBannerNDao;
import com.ld.entity.BannerN;
import com.ld.entity.BannerNew;
import org.jongo.Jongo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class TestService {

    @Autowired
    private MongoBannerNDao mongoBannerNDao;
    @Autowired
    private MysqlBannerNDao mysqlBannerNDao;
    @Autowired
    private Jongo jongo;

    public void transfer(){

       List<BannerN> list =  mongoBannerNDao.getList();
//        List<BannerNew> finalList = new ArrayList<>();
//        jongo.getCollection("BannerN").find().as(BannerNew.class).forEach(bannerN -> finalList.add(bannerN));
//       if(CollectionUtils.isEmpty(finalList)){
//           System.out.println(123113213);
//           return;
//       }
//        String aaa = JSONArray.toJSONString(finalList,SerializerFeature.UseISO8601DateFormat);
//        File file =new File("BannerN.json");
//        try {
//        //if file doesnt exists, then create it
//        if(!file.exists()){
//                file.createNewFile();
//        }
//            //true = append file
//            FileWriter fileWritter = new FileWriter(file.getName(),false);
//            fileWritter.write(aaa);
//            fileWritter.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        List<BannerNew> finalList = new ArrayList<>();
        try {
            File file =new File("BannerN.json");
            FileInputStream fip = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
            StringBuffer sb = new StringBuffer();
            while(reader.ready()){
                //将读取的数据转化成char类型，加入StringBuffer对象sb里
                sb.append((char)reader.read());

            }
            //将sb对象内容转化成string类型，输出
            System.out.println(sb.toString());

            //关闭读取流
            reader.close();

            //关闭输入流，释放系统资源
            fip.close();
            finalList = JSONArray.parseArray(sb.toString(),BannerNew.class);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
        }
        for (BannerNew bannerN:finalList) {
            BannerN n = new BannerN();

             BeanUtils.copyProperties(bannerN,n);
            System.out.println(JSONObject.toJSONString(n));
            mysqlBannerNDao.save(n);
        }
    }

    public void getInfo(){
        List<BannerN> bannerNS = mysqlBannerNDao.findAll();
        for (BannerN bannerN: bannerNS){
            System.out.println(JSONObject.toJSONString(bannerN, SerializerFeature.WRITE_MAP_NULL_FEATURES,SerializerFeature.QuoteFieldNames,SerializerFeature.UseISO8601DateFormat));
        }
    }
}
